import 'dart:convert';

import 'package:find_friend/const/const.dart';
import 'package:find_friend/pages/xspace/model/school_model.dart';
import 'package:rongcloud_im_plugin/rongcloud_im_plugin.dart';
import 'package:shared_preferences/shared_preferences.dart';


class SharedUserInfo {
  String mobile;
  String nickname;
  int gender;
  String birthday;
  String bio;
  String money;
  int score;
  int level;
  String avatar;
  String idcard;
  // String birJson;
  String zodiac;
  String zodiacC;
  String wuxing;
  bool password; // 是否有密码
  int schoolId;
  String schoolname;
  List<FileModel> photowall;
  List<FileModel> sticker;
  List<String> label;// 用户标签
  Voice signvoice;
  String longback;// 用户背景长图
  int followflag; // 关注标识
  String group; // 好友分组名字
  int group_id; // 好友分组id
  String friendgroup;
  String chatback;// 好友聊天背景
  String followRemark;
  String xzonet;

  String get remarkname {
    if(nickname == null && followRemark == null) return mobile;
    return followRemark == null ? nickname : followRemark;
  }

  bool get follow {
    return followflag != 0;
  }


  static Future<SharedUserInfo> instance() async {
    SharedPreferences share = await SharedPreferences.getInstance();
    String j = share.getString(KEY_userjosn_info);
    SharedUserInfo user = SharedUserInfo.fromJson(json.decode(j));
    return user;
  }

  static Future<void> saveData(dynamic data, {isupdateIM = false}) async {
    SharedPreferences share = await SharedPreferences.getInstance();
    String tempJS = json.encode(data["data"]);
    share.remove(KEY_userjosn_info);
    share.setString(KEY_userjosn_info, tempJS);

    if(isupdateIM) { // 更新融云用户信息
      final map = data["data"];
      if(map != null){
        RongcloudImPlugin.updateCurrentUserInfo(map["mobile"], map["nickname"], map["avatar"]);
      }
    }
  }

  SharedUserInfo(
      {this.mobile,
      this.nickname,
      this.gender,
      this.birthday,
      this.bio,
      this.money,
      this.score,
      this.level,
      this.avatar,
      this.idcard,
      // this.birJson,
      this.zodiac,
      this.followflag,
      this.zodiacC,
      this.wuxing,
      this.password,
      this.schoolId,
      this.schoolname,
      this.photowall,
      this.signvoice,
      this.longback,
      this.label,
      this.group,
      this.group_id,
      this.sticker,
      this.friendgroup,
      this.followRemark,
      this.chatback
    });

  SharedUserInfo.fromJson(Map<String, dynamic> json) {
    mobile = json['mobile'];
    nickname = json['nickname'];
    followRemark = json["follow_remark"];
    gender = json['gender'];
    birthday = json['birthday'];
    bio = json['bio'];
    money = json['money'];
    score = json['score'];
    level = json['level'];
    avatar = json['avatar'];
    idcard = json['idcard'];
    // birJson = json['bir_json'];
    zodiac = json['zodiac'];
    zodiacC = json['zodiac_c'];
    wuxing = json['wuxing'];
    password = json['password'];
    schoolId = json['school_id'];
    schoolname = json['schoolname'];
    longback = json['longback'];
    group_id = json['group_id'];
    group = json["group"];
    friendgroup = json["friendgroup"];
    chatback = json['chatback'];
    followflag = json['followflag'];
    if(json["label"] != null){
      label = json['label'].cast<String>();
    }
    if (json['photowall'] != null) {
      photowall = new List<FileModel>();
      json['photowall'].forEach((v) {
        photowall.add(new FileModel.fromJson(v));
      });
    }
    if (json['sticker'] != null) {
      sticker = new List<FileModel>();
      json['sticker'].forEach((v) {
        sticker.add(new FileModel.fromJson(v));
      });
    }
    this.signvoice = json['signvoice'] != null
        ? new Voice.fromJson(json['signvoice'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['mobile'] = this.mobile;
    data['nickname'] = this.nickname;
    data['gender'] = this.gender;
    data['birthday'] = this.birthday;
    data['bio'] = this.bio;
    data['money'] = this.money;
    data['score'] = this.score;
    data['followflag'] = this.followflag;
    data['level'] = this.level;
    data['avatar'] = this.avatar;
    data['idcard'] = this.idcard;
    // data['bir_json'] = this.birJson;
    data['zodiac'] = this.zodiac;
    data['zodiac_c'] = this.zodiacC;
    data['wuxing'] = this.wuxing;
    data['password'] = this.password;
    data['school_id'] = this.schoolId;
    data['schoolname'] = this.schoolname;
    data['longback'] = this.longback;
    data['signvoice'] = this.signvoice;
    data['label'] = this.label;
    data['group'] = this.group;
    data["group_id"] = this.group_id;
    data['chatback'] = this.chatback;
    data["friendgroup"] = this.friendgroup;
    data['follow_remark'] = this.followRemark;
    
    if (this.photowall != null) {
      data['photowall'] = this.photowall.map((v) => v.toJson()).toList();
    }

    if (this.sticker != null) {
      data['sticker'] = this.sticker.map((v) => v.toJson()).toList();
    }
    
    if (this.signvoice != null) {
      data['signvoice'] = this.signvoice.toJson();
    }
    return data;
  }
}

class FileModel {
  int id;
  String url;

  // 音频链接x区会用到
  String audiourl; 
  int duration; // 音频播放的时间

  FileModel({this.id, this.url, this.duration, this.audiourl});

  FileModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    url = json['url'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['url'] = this.url;
    return data;
  }
}